package cn.edu.ncu.housinghub.service.impl;

import cn.edu.ncu.housinghub.constant.ErrorCodeConstant;
import cn.edu.ncu.housinghub.entity.house.PropertyVerification;
import cn.edu.ncu.housinghub.entity.user.Landlord;
import cn.edu.ncu.housinghub.entity.user.User;
import cn.edu.ncu.housinghub.exception.BusinessException;
import cn.edu.ncu.housinghub.mapper.admin.AdminHouseMapper;
import cn.edu.ncu.housinghub.service.AdminHouseService;
import cn.edu.ncu.housinghub.service.HouseService;
import cn.edu.ncu.housinghub.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminHouseServiceImpl implements AdminHouseService {
    @Autowired
    private AdminHouseMapper adminHouseMapper;
    @Autowired
    private HouseService houseService;
    @Autowired
    private UserService userService;

    /**
     * 管理员通过房产认证,并将普通用户转为房东
     */
    @Transactional
    @Override
    public void modifyPropertyVerificationStatus(Long propertyVerifyId) {
        //1、根据propertyVerifyId查询认证信息和用户信息
        PropertyVerification verification =houseService.findPropertyVerificationById(propertyVerifyId);
        User user = userService.findById(verification.getUserId());
        if (verification==null){
            throw new BusinessException(ErrorCodeConstant.PROPERTY_VERIFICATION_NOT_EXIST);
        }
        //1、判断认证信息是否处于待审核状态
        if(verification.getVerifyStatus()!=0){
            throw new BusinessException(ErrorCodeConstant.PROPERTY_VERIFY_STATUS_MISMATCH);
        }

        //2、判断用户角色是否为普通用户，若为普通用户则修改为房东角色并写入房东表
        if(user.getRole() == 1){
            userService.modifyUserRoleToLandlord(user.getId());
            userService.addNewLandlord(new Landlord(user.getId()));
        }

        //3、修改认证信息状态为已通过

        adminHouseMapper.updatePropertyVerificationStatus(propertyVerifyId);
    }

    /*
    * 获取所有房产认证信息
     */
    @Override
    public List<PropertyVerification> findAllPropertyVerification() {
        return adminHouseMapper.selectAllPropertyVerification();
    }

    /*
    * 批量通过房产认证
     */
    @Override
    public void modifyBatchPassVerify(List<Long> propertyVerifyIds) {
        for (Long propertyVerifyId : propertyVerifyIds) {
            PropertyVerification verification =houseService.findPropertyVerificationById(propertyVerifyId);
            if(verification.getVerifyStatus()!=0){
                throw new BusinessException(ErrorCodeConstant.PROPERTY_VERIFY_STATUS_MISMATCH);
            }
            if(verification==null||verification.getUserId()==null){
                throw new BusinessException(ErrorCodeConstant.PROPERTY_VERIFICATION_NOT_EXIST);
            }
            User user = userService.findById(verification.getUserId());
            if(user.getRole() == 1){
                userService.modifyUserRoleToLandlord(user.getId());
                userService.addNewLandlord(new Landlord(user.getId()));
            }
        }
        adminHouseMapper.updateBatchPassVerify(propertyVerifyIds);
    }

    /*
    * 拒绝房产认证
     */
    @Override
    public void modifyRejectVerify(Long propertyVerifyId,String reason) {
        PropertyVerification verification =houseService.findPropertyVerificationById(propertyVerifyId);
        //1、判断认证信息是否存在
        if (verification==null){
            throw new BusinessException(ErrorCodeConstant.PROPERTY_VERIFICATION_NOT_EXIST);
        }

        //2、判断认证信息是否处于待审核状态
        if(verification.getVerifyStatus()!=0){
            throw new BusinessException(ErrorCodeConstant.PROPERTY_VERIFY_STATUS_MISMATCH);
        }


        adminHouseMapper.updatePropertyVerificationReject(propertyVerifyId,reason);
    }
}
